﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JamesThew.Dashboard
{
    public partial class _default : System.Web.UI.Page
    {
        public UserDB udb;
        public User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["UserLogin"] != null && Session["UserLevel"] != null)
                {
                    if (Session["UserLevel"].ToString() != "1")
                    {
                        Response.Redirect("../default.aspx");
                    }
                    else
                    {
                        Response.Redirect("Statistics.aspx");
                    }
                }
            }
            catch (Exception ex) {
                Response.Redirect("../default.aspx");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.udb = new UserDB();

            String UserLogin = txtUserName.Text;
            String UserPass = txtPassword.Text;
            if (UserLogin.Equals("") || UserPass.Equals(""))
            {
                Response.Write("Please enter user name and password");
            }
            else
            {
                this.user = udb.Login(UserLogin, UserPass);
                if (this.user.UserLogin == null || this.user.UserLogin == "")
                {
                    Response.Write("Wrong Username or Password!");
                }
                else
                {
                    Session.Add("UserLogin", user.UserLogin);
                    Session.Add("UserLevel", user.UserLevel);
                    Session.Add("UserID", user.UserID);

                    if (user.UserLevel == 1)
                    {
                        Response.Redirect("Statistics.aspx");
                    }
                    else 
                    {
                        Response.Redirect("../default.aspx");
                    }
                }
            }
        }
    }
}